package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.RideUseLog;
import com.tbit.uqbike.object.pojo.dto.RideOrderDTO;
import com.tbit.uqbike.object.pojo.vo.RideUseLogVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.RideUseLogService;
import com.tbit.uqbike.webmanager.dao.capital.RideUseLogDao;
import com.tbit.uqbike.webmanager.util.csv.CsvExportUtils;
import com.tbit.uqbike.webmanager.util.csv.entity.ExportHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.IntSupplier;

/**
 * 骑行订单金额分布接口实现类
 *
 * @Author:BUGTian
 * @DATE: 2023/1/7
 */
@Service
public class RideUseLogServiceImpl extends ServiceImpl<RideUseLogDao, RideUseLog> implements RideUseLogService {
    @Autowired
    private RideUseLogDao rideUseLogDao;
    @Autowired
    private CsvExportUtils csvExportUtils;

    @Override
    public PageResult<RideUseLogVO> queryPage(Integer page, Integer size, Integer brandId, String accountIds, String orderNO, Date startTime, Date endTime) {
        PageInfo<RideUseLogVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> rideUseLogDao.queryList(brandId, accountIds, orderNO, startTime, endTime));
        return new PageResult<>(page, size, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public String exportCsv(RideOrderDTO rideOrderDTO) {
        rideOrderDTO.setFileName("骑行订单金额分布");
        File tempFile = csvExportUtils.getTempFile();
        try {
            File file = download(rideOrderDTO, tempFile);
            String uploadFileName = UUID.randomUUID().toString().replace("-", "");
            return csvExportUtils.uploadZip(tempFile, uploadFileName, file);
        } finally {
            FileUtil.del(tempFile);
        }
    }

    @Override
    public List<RideUseLogVO> export(Integer brandId, String accountIds, String orderNO, Date startTime, Date endTime) {
        PageInfo<RideUseLogVO> pageInfo = PageHelper.startPage(1, 65535).doSelectPageInfo(() -> rideUseLogDao.queryList(brandId, accountIds, orderNO, startTime, endTime));
        if (pageInfo.getTotal() > 65535) {
            throw new BaseException("导出数量过大，请调整查询范围");
        }
        List<RideUseLogVO> list = pageInfo.getList();
        for (RideUseLogVO rideUseLogVO : list) {
            rideUseLogVO.setTotalMoneyYuan(rideUseLogVO.getTotalMoneyYuan());
            rideUseLogVO.setBalanceYuan(rideUseLogVO.getBalanceYuan());
            rideUseLogVO.setBalanceRefundYuan(rideUseLogVO.getBalanceRefundYuan());
            rideUseLogVO.setOtherYuan(rideUseLogVO.getOtherYuan());
            rideUseLogVO.setOtherRefundYuan(rideUseLogVO.getOtherRefundYuan());
        }
        return list;
    }

    public File download(RideOrderDTO rideOrderDTO, File tempFile) {
        IntSupplier countFn = () -> rideUseLogDao.queryListCount(rideOrderDTO.getBrandId(), rideOrderDTO.getAccountIds(), rideOrderDTO.getOrderNO(), rideOrderDTO.getStartTime(), rideOrderDTO.getEndTime());
        ExportHandler<RideUseLogVO> exportHandler = baseEntity -> {
            List<RideUseLogVO> voList = rideUseLogDao.queryList(rideOrderDTO.getBrandId(), rideOrderDTO.getAccountIds(),
                    rideOrderDTO.getOrderNO(), baseEntity.getStartTime(), baseEntity.getEndTime());
            for (RideUseLogVO rideUseLogVO : voList) {
                rideUseLogVO.setTotalMoneyYuan(rideUseLogVO.getTotalMoneyYuan());
                rideUseLogVO.setBalanceYuan(rideUseLogVO.getBalanceYuan());
                rideUseLogVO.setBalanceRefundYuan(rideUseLogVO.getBalanceRefundYuan());
                rideUseLogVO.setOtherYuan(rideUseLogVO.getOtherYuan());
                rideUseLogVO.setOtherRefundYuan(rideUseLogVO.getOtherRefundYuan());
            }
            return voList;
        };
        return csvExportUtils.exportBigCsvUseDynamicDate(exportHandler, RideUseLogVO.class, countFn,
                rideOrderDTO.getStartTime(), rideOrderDTO.getEndTime(), tempFile, rideOrderDTO.getFileName());
    }

}
